home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 467 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  109 lines

  1. Path: solon.com!not-for-mail
  2. From: rastrl@cs.buffalo.edu (Robert L Rast)
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated
  4. Subject: Re: Leading and Trailing Blanks
  5. Date: 5 Jan 1996 09:54:04 -0600
  6. Organization: State University of New York at Buffalo/Computer Science
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4cjhis$fb1@solutions.solon.com>
  10. References: <4chh1b$685@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13.  
  14. In article <4chh1b$685@solutions.solon.com>,
  15. Casey Claiborne <mskc@io.com> wrote:
  16. >
  17. >
  18. >Hello -
  19. >    I am wondering if anyone out there has a program (or knows of one)
  20. >that allows one to strip leading and trailing blanks from a string. 
  21. >ex:
  22. >
  23. >    char test[20];
  24. >    strcpy(test,"  TESTING  ");
  25. >    printf("%s", test);
  26. >
  27. >will produce an output like
  28. >  TESTING  
  29. >
  30. >that has blanks at the beginning of "TESTING". I would like to 
  31. >have the following result
  32. >
  33. >TESTING
  34. >
  35. >that has no leading blanks.
  36. >
  37.  
  38.  
  39. This might work:
  40.  
  41.  
  42.  
  43. #include <stdio.h>
  44. #include <string.h>
  45.  
  46. char * tstr = "  TESTING  ";
  47. char * strip (char * dest, char * src);
  48.  
  49.  
  50. int main (void) {
  51.  
  52.   char result[256];
  53.  
  54.   strip(result, tstr);
  55.   printf("tstr=%s.\t length = %d\n", tstr, strlen(tstr));
  56.   printf("result=%s.\t length = %d\n", result, strlen(result));
  57.   return 0;
  58. }
  59. }
  60.  
  61.  
  62.  
  63. char * strip (char * dest, char * src) {
  64.  
  65.   char * fptr, * bptr;  /* front and back ptrs */
  66.  
  67.   if (NULL == src || '\0' == *src) {
  68.     *dest = '\0';
  69.     return src;
  70.   }
  71.   fptr = src;
  72.   bptr = src + strlen(src) - 1;
  73.   while(' ' == *fptr && '\0' != *fptr) ++fptr;
  74.   while(' ' == *bptr && bptr > src) --bptr;
  75.   if (bptr >= fptr) {
  76.     int len = 1 + (bptr-fptr);
  77.     strncpy(dest, fptr, len);
  78.     *(dest + len) = '\0';
  79.   }
  80.   else 
  81.     *dest = '\0';
  82.   return dest;
  83. }
  84. }
  85.   
  86. /* end */
  87.  
  88.  
  89.  
  90. It was tested with null and zero-length strings.  Also, a string with
  91.  
  92. all spaces succeeded. You could probably substitute "isspace(*ptr)" for 
  93.  
  94. the ' ' condition in the while loops - this will take care of tabs, etc.
  95.  
  96. Hope you like it, it's all I got.
  97.  
  98.  
  99. Psychovoid 2007
  100.  
  101.  
  102.     "Just because it's bad doesn't mean it won't be popular. After
  103.     all, it's Microsoft."     ___
  104.                             __   (.x.)   __
  105. --rastrl@acsu.buffalo.edu--(,,^\--\-/--/^,,)--www.acsu.buffalo.edu/~rastrl--  
  106.  
  107.  
  108.   
  109.